<?php
/**
* @version $Id: admin.news.html.php 10002 2008-02-08 10:56:57Z willebil $
* @package Joomla
* @subpackage news
* @copyright Copyright (C) 2005 Open Source Matters. All rights reserved.
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL, see LICENSE.php
* Joomla! is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.php for copyright notices and details.
*/

// no direct access
defined( '_VALID_MOS' ) or die( 'Restricted access' );

/**
* @package Joomla
* @subpackage news
*/
class HTML_News {

	/**
	* Writes a list of the news items
	* @param array An array of news objects
	*/
	function showNews( &$rows, $section, &$lists, $search, $pageNav, $all=NULL, $redirect ) {
		global $my, $acl, $database, $mosConfig_offset;

		mosCommonHTML::loadOverlib();
		?>
		<form action="index2.php?option=com_news" method="post" name="adminForm">

		<table class="adminheading">
		<tr>
			<th class="edit" rowspan="2" nowrap="nowrap">
			<?php
			if ( $all ) {
				?>
				Quản trị tin khuyến mãi <small><small>[ Section: All ]</small></small>
				<?php
			} else {
				?>
				Quản trị tin khuyến mãi <small><small>[ Section: <?php echo $section->title; ?> ]</small></small>
				<?php
			}
			?>
			</th>
			<?php
			if ( $all ) {
				?>
				<td align="right" rowspan="2" valign="top">
				<?php echo $lists['sectionid'];?>
				</td>
				<?php
			}
			?>
			<td align="right" valign="top">
			<?php echo $lists['catid'];?>
			</td>
			<td valign="top">
			<?php echo $lists['authorid'];?>
			</td>
		</tr>
		<tr>
			<td align="right">
			Tìm theo từ khóa:
			</td>
			<td>
			<input type="text" name="search" value="<?php echo htmlspecialchars( $search );?>" class="text_area" onChange="document.adminForm.submit();" />
			</td>
		</tr>
		</table>

		<table class="adminlist">
		<tr>
			<th width="5">
			#
			</th>
			<th width="5">
			<input type="checkbox" name="toggle" value="" onclick="checkAll(<?php echo count( $rows ); ?>);" />
			</th>
			<th class="title">
			Tên chương trình
			</th>
			<th width="5%">
			Published
			</th>
			<th width="2%">
			ID
			</th>
			<?php
			if ( $all ) {
				?>
				<th align="left">
				Section
				</th>
				<?php
			}
			?>
			<th align="left">
			Category
			</th>
			<th align="left">
			Quản trị
			</th>
			<th align="center" width="10">
			Ngày
			</th>
		  </tr>
		<?php
		$k = 0;
		$nullDate = $database->getNullDate();
		for ($i=0, $n=count( $rows ); $i < $n; $i++) {
			$row = &$rows[$i];

			mosMakeHtmlSafe($row);

			$link 	= 'index2.php?option=com_news&sectionid='. $redirect .'&task=edit&hidemainmenu=1&id='. $row->id;

			$row->sect_link 	= 'index2.php?option=com_sections&task=editA&hidemainmenu=1&id='. $row->sectionid;
			$row->cat_link 		= 'index2.php?option=com_categories&task=editA&hidemainmenu=1&id='. $row->catid;

			$now = _CURRENT_SERVER_TIME;
			if ( $row->state == 1 ) {
			// Published
				$img = 'publish_g.png';
				$alt = 'Published';
			}elseif ( $row->state == 0 ) {
			// Unpublished
				$img = 'publish_x.png';
				$alt = 'Unpublished';
			}
			
			if ( $acl->acl_check( 'administration', 'manage', 'users', $my->usertype, 'components', 'com_users' ) ) {
				if ( $row->created_by_alias ) {
					$author = $row->created_by_alias;
				} else {
					$linkA 	= 'index2.php?option=com_users&task=editA&hidemainmenu=1&id='. $row->created_by;
					$author = '<a href="'. $linkA .'" title="Edit User">'. $row->author .'</a>';
				}
			} else {
				if ( $row->created_by_alias ) {
					$author = $row->created_by_alias;
				} else {
					$author = $row->author;
				}
			}

			$date = mosFormatDate( $row->created, '%x' );

			$access 	= mosCommonHTML::AccessProcessing( $row, $i );
			$checked 	= mosCommonHTML::CheckedOutProcessing( $row, $i );
			?>
			<tr class="<?php echo "row$k"; ?>">
				<td>
				<?php echo $pageNav->rowNumber( $i ); ?>
				</td>
				<td align="center">
				<?php echo $checked; ?>
				</td>
				<td>
				<?php
				if ( $row->checked_out && ( $row->checked_out != $my->id )) {
					echo $row->title;
				} else {
					?>
					<a href="<?php echo $link; ?>" title="Edit news">
					<?php echo $row->title; ?>
					</a>
					<?php
				}
				?>
				</td>
				<td align="center">
				<a href="javascript: void(0);" onMouseOver="return overlib('<table><?php echo $times; ?></table>', CAPTION, 'Publish Information', BELOW, RIGHT);" onMouseOut="return nd();" onclick="return listItemTask('cb<?php echo $i;?>','<?php echo $row->state ? "unpublish" : "publish";?>')">
				<img src="images/<?php echo $img;?>" width="12" height="12" border="0" alt="<?php echo $alt; ?>" />
				</a>
				</td>
				<td align="left">
				<?php echo $row->id; ?>
				</td>
				<?php
				if ( $all ) {
					?>
					<td align="left">
					<a href="<?php echo $row->sect_link; ?>" title="Edit Section">
					<?php echo $row->section_name; ?>
					</a>
					</td>
					<?php
				}
				?>
				<td align="left">
				<a href="<?php echo $row->cat_link; ?>" title="Edit Category">
				<?php echo $row->name; ?>
				</a>
				</td>
				<td align="left">
				<?php echo $author; ?>
				</td>
				<td align="left">
				<?php echo $date; ?>
				</td>
			</tr>
			<?php
			$k = 1 - $k;
		}
		?>
		</table>

		<?php echo $pageNav->getListFooter(); ?>

		<input type="hidden" name="option" value="com_news" />
		<input type="hidden" name="sectionid" value="<?php echo $section->id;?>" />
		<input type="hidden" name="task" value="" />
		<input type="hidden" name="boxchecked" value="0" />
		<input type="hidden" name="hidemainmenu" value="0" />
		<input type="hidden" name="redirect" value="<?php echo $redirect;?>" />
		<input type="hidden" name="<?php echo josSpoofValue(); ?>" value="1" />
		</form>
		<?php
	}


	/**
	* Writes the edit form for new and existing news item
	*
	* A new record is defined when <var>$row</var> is passed with the <var>id</var>
	* property set to 0.
	* @param mosnews The category object
	* @param string The html for the groups select list
	*/
	function editNews( &$row, $section, $sectionid, &$lists, &$sectioncategories, &$params, $option, $redirect, &$lsContentNews, $lsIDContent ) {
		global $database, $mosConfig_live_site, $mosConfig_absolute_path;
		
		mosMakeHtmlSafe( $row );
		mosCommonHTML::loadOverlib();
		mosCommonHTML::loadCalendar();
		$rowid = intval($row->id);
		?>
		<script language="javascript" type="text/javascript">
		var sectioncategories = new Array;
		<?php
		$i = 0;
		foreach ($sectioncategories as $k=>$items) {
			foreach ($items as $v) {
				echo "sectioncategories[".$i++."] = new Array( '$k','".addslashes( $v->id )."','".addslashes( $v->name )."' );\t";
			}
		}
		?>
		function submitbutton(pressbutton) {
			var form = document.adminForm;

			if (pressbutton == 'cancel') {
				submitform( pressbutton );
				return;
			}
			// do field validation
			if (form.title.value == ""){
				alert( "news item must have a title" );
			} else if (form.sectionid.value == "-1"){
				alert( "Vui lòng chọn nhóm sản phẩm." );
			} else if (form.catid.value == "-1"){
				alert( "Vui lòng lựa chọn tên nhóm sản phẩm." );
 			} else if (form.catid.value == ""){
 				alert( "Vui lòng lựa chọn tên nhóm sản phẩm." );
			}
//			else if (form.catphone_id.value == ""){
// 				alert( "Vui lòng chọn hãng sản xuất." );
//			}
			 else {
				<?php getEditorContents( 'editor1', 'introtext' ) ; ?>
				<?php getEditorContents( 'editor2', 'fulltext' ) ; ?>
				submitform( pressbutton );
			}
		}
		//-->
		</script>
		<script type="text/javascript" src="<?php echo $mosConfig_live_site;?>/includes/jquery_upload_crop/js/jquery-pack.js"></script>
		<script type="text/javascript" src="<?php echo $mosConfig_live_site;?>/includes/jquery_upload_crop/js/jquery.imgareaselect-0.3.min.js"></script>

		<form action="index2.php" method="post" name="adminForm" enctype="multipart/form-data">
		<table class="adminheading">
		<tr>
			<th class="edit">
			Chi tiết tin khuyến mãi:
			<small>
			<?php echo $row->id ? 'Edit' : 'New';?>
			</small>
			<?php
			if ( $row->id ) {
				?>
				<small><small>
				[ Danh mục: <?php //echo $section; ?> ]
				</small></small>
				<?php
			}
			?>
			</th>
		</tr>
		</table>

		<table cellspacing="0" cellpadding="0" width="100%">
		<tr>
			<td width="50%" valign="top">
				<table width="100%" class="adminform">
				<tr>
					<td width="100%">
						<table cellspacing="0" cellpadding="0" border="0" width="100%">
						<tr>
							<th colspan="4">
							THÔNG TIN CHI TIẾT
							</th>
						</tr>
						<tr>
							<td>
							Tên sản phẩm:
							</td>
							<td>
							<input class="text_area" type="text" name="title" size="30" maxlength="100" value="<?php echo $row->title; ?>" />
							</td>
							<td>
							Thuộc vùng:
							</td>
							<td>
							<?php echo $lists['sectionid']; ?>
							</td>
						</tr>
						<tr>
							<td>
							Seo tên:
							</td>
							<td>
							<input name="title_alias" type="text" class="text_area" id="title_alias" value="<?php echo $row->title_alias; ?>" size="30" maxlength="100" disabled="disabled" />
							</td>
							<td>
							Thuộc nhóm:
							</td>
							<td>
							<?php echo $lists['catid']; ?>
							</td>
						</tr>
						<?php if ($row->id==0 || ($row->largeImage=='' || !$row->largeImage)){?>
						<tr>
							<td>
							Chọn ảnh:
							</td>
							<td>
							 <input type="file" name="image" size="30" />
							</td>
							<td>
							Upload ảnh:
							</td>
							<td>
							<input type="button" name="upload" value="Upload" onclick="javascript:submitbutton('upload');" />
							</td>
						</tr>
						<?php }?>
						<tr>
							<td colspan="4">
							<?php
							$upload_dir = $mosConfig_live_site."/uploads/temp";
							$upload_path = $upload_dir."/";
							$large_image_name = "temp_pic.jpg";
							$max_file = "1148576";
							$max_width = "500";
							$thumb_width = "80";
							$thumb_height = "60";
							
							//Image Locations
							$large_image_location = $upload_path.$large_image_name;
							
							//Create the upload directory with the right permissions if it doesn't exist
							if(!is_dir($upload_dir)){
								mkdir($upload_dir, 0777);
								//chmod($upload_dir, 0777);
							}
							
							//Check to see if any images with the same names already exist
							if (file_exists($large_image_location)){
							   	$large_photo_exists = "<img src=\"".$upload_path.$large_image_name."\" alt=\"Large Image\"/>";
							} else {
							   	$large_photo_exists = "";
							}
							
							//Only display the javacript if an image has been uploaded
//							if(strlen($large_photo_exists)>0){
								//$current_large_image_width = getWidth($large_image_location);
								//$current_large_image_height = getHeight($large_image_location);
								$current_large_image_width = 450;
								$current_large_image_height = 320;
								?>
								<script type="text/javascript">
								function preview(img, selection) { 
									var scaleX = <?php echo $thumb_width;?> / selection.width; 
									var scaleY = <?php echo $thumb_height;?> / selection.height; 
									
									$('#thumbnail + div > img').css({ 
										width: Math.round(scaleX * <?php echo $current_large_image_width;?>) + 'px', 
										height: Math.round(scaleY * <?php echo $current_large_image_height;?>) + 'px',
										marginLeft: '-' + Math.round(scaleX * selection.x1) + 'px', 
										marginTop: '-' + Math.round(scaleY * selection.y1) + 'px' 
									});
									$('#x1').val(selection.x1);
									$('#y1').val(selection.y1);
									$('#x2').val(selection.x2);
									$('#y2').val(selection.y2);
									$('#w').val(selection.width);
									$('#h').val(selection.height);
								} 
								<?php if ($row->id==0 || ($row->largeImage=='' || !$row->largeImage)){?>
								$(document).ready(function () { 
									$('#save_thumb').click(function() {
										var x1 = $('#x1').val();
										var y1 = $('#y1').val();
										var x2 = $('#x2').val();
										var y2 = $('#y2').val();
										var w = $('#w').val();
										var h = $('#h').val();
										if(x1=="" || y1=="" || x2=="" || y2=="" || w=="" || h==""){
											alert("Vui lòng lựa chọn ảnh đại điện cho sản phẩm của bạn bằng cách bấm vào ảnh và chọn vùng ảnh bạn muốn !");
											return false;
										}else{
											return true;
										}
									});
								}); 
								<?php }?>
								
								$(window).load(function () { 
									$('#thumbnail').imgAreaSelect({ aspectRatio: '1:1', onSelectChange: preview }); 
								});
								</script>
								<?php
								if ($row->id==0 || ($row->largeImage=='' || !$row->largeImage)){
								?>
								<img src="<?php echo $upload_path.$large_image_name;?>" style="float: left; margin-right: 10px;" id="thumbnail" alt="Tạo ảnh đại diện" />
								<div style="float:left; position:relative; overflow:hidden; width:<?php echo $thumb_width;?>px; height:<?php echo $thumb_height;?>px;">
									<img src="<?php echo $upload_path.$large_image_name;?>" style="position: relative;" alt="Ảnh đại diện" />
								</div>
								<?php 
								}else{
									if ($row->largeImage=='' || !$row->largeImage){
									?>
										<img id="pic"  src="<?php echo $mosConfig_live_site;?>/uploads/no_pic.gif" width="160" border="0" />
										<?php
									}
									else{
										?>
										<img id="pic" src="<?php echo $mosConfig_live_site;?>/uploads/news/large/<?php echo $row->largeImage;?>" width="160" border="0" />
										<?php
									}
									if ($row->mediumImage=='' || !$row->mediumImage){
									?>
										<img id="pic"  src="<?php echo $mosConfig_live_site;?>/uploads/no_pic.gif" width="120" border="0" />
										<?php
									}
									else{
										?>
										<img id="pic" src="<?php echo $mosConfig_live_site;?>/uploads/news/medium/<?php echo $row->mediumImage;?>" width="120" border="0" />
										<?php
									}
									if ($row->thumbnailImage=='' || !$row->thumbnailImage){
									?>
										<img id="pic"  src="<?php echo $mosConfig_live_site;?>/uploads/no_pic.gif" width="80" border="0" />
										<?php
									}
									else{
										?>
										<img id="pic" src="<?php echo $mosConfig_live_site;?>/uploads/news/thumbnail/<?php echo $row->thumbnailImage;?>" width="80" border="0" />
										<?php
									}
									if ($row->id!=0 && $row->largeImage!=''){
									?>
									<input type="button" name="delete" value="Xóa ảnh" onclick="javascript:submitbutton('delImage');" />
									<?php
									}
								}
								?>
							</td>
						</tr>
						<tr>
							<td>
							</td>
							<td>
							</td>
							<td>
							Hãng sản xuất:
							</td>
							<td>
							<select class="inputbox" name="catphone_id">
								<?php
								if ($rowid==0){
									echo "<option value=''>--- Chọn hãng sản xuất ---</option>";
									$sql = "SELECT id,name FROM #__catphone WHERE published=1 ORDER BY name ASC, ordering ASC";
									$database->setQuery($sql);
									$listPhone = $database->loadObjectList();
									for ($i=0; $i<count($listPhone); $i++){
										echo "<option value='".$listPhone[$i]->id."'>".$listPhone[$i]->name."</option>";
									}
								}
								else{
									$sql = "SELECT id,name FROM #__catphone WHERE published=1 AND id=".$row->catphone_id;
									$database->setQuery($sql);
									$database->loadObject($Phoneted);
									echo "<option value='".$Phoneted->id."' selected>".$Phoneted->name."</option>";
									$sql = "SELECT id,name FROM #__catphone WHERE published=1 AND id!=".$row->catphone_id." ORDER BY name ASC, ordering ASC";
									$database->setQuery($sql);
									$listPhone = $database->loadObjectList();
									for ($i=0; $i<count($listPhone); $i++){
										echo "<option value='".$listPhone[$i]->id."'>".$listPhone[$i]->name."</option>";
									}
								}
								?>
							</select>
							</td>
						</tr>
						<?php
						$time = time();					
						if ($rowid==0){
							$ngay_nhap 			= date('Y-m-d H:i:s',$time);
							$ngay_nhap_start 	= date('Y-m-d H:i:s',$time);
							$ngay_nhap_end	 	= date('Y-m-d H:i:s',$time);
						}else{
							$ngay_nhap 			= $row->created;
							$ngay_nhap_start 	= $row->start_date;
							$ngay_nhap_end	 	= $row->end_date;
						}
						?>		
						<tr>
							<td>
							Ngày bắt đầu:
							</td>
							<td>
							<input class="text_area" type="text" name="start_date" id="start_date" size="25" maxlength="19" value="<?php echo $ngay_nhap_start; ?>" />
							<input name="reset" type="reset" class="button" onclick="return showCalendar('start_date', 'y-mm-dd');" value="..." />
							</td>
							<td>
							Ngày kết thúc:
							</td>
							<td>
							<input class="text_area" type="text" name="end_date" id="end_date" size="25" maxlength="19" value="<?php echo $ngay_nhap_end; ?>" />
							<input name="reset" type="reset" class="button" onclick="return showCalendar('end_date', 'y-mm-dd');" value="..." />
							</td>
						</tr>
						<tr>
							<td>
							Định dạng ngày:
							</td>
							<td>
							<b>(Năm-tháng-ngày Giờ:phút:giây)</b>
							</td>
							<td>
							</td>
							<td>
							<b>(Năm-tháng-ngày Giờ:phút:giây)</b>
							</td>
						</tr>
						</table>
					</td>
				</tr>
				<tr>
					<td width="100%">
					Mô tả ngắn:
					<br /><?php
					// parameters : areaname, news, hidden field, width, height, rows, cols
					//editorArea( 'editor2', $row->introtext, 'introtext', '100%;', '250', '55', '30' ) ; ?>
					<textarea rows="5" cols="60" name="introtext"><?php echo $row->introtext;?></textarea>
					</td>
				</tr>
				<tr>
					<td width="100%">
					Mô tả chi tiết:
					<br /><?php
					// parameters : areaname, news, hidden field, width, height, rows, cols
					editorArea( 'editor2', $row->fulltext, 'fulltext', '100%;', '500', '85', '30' ) ; ?>
					</td>
				</tr>
				</table>
			</td>
			<td valign="top" width="50%">
				<?php
				//$tabs->startPane("news-pane");
				//$tabs->startTab("Publishing","publish-page");
				?>
				<table class="adminform">
				<tr>
					<th colspan="2">
					THÔNG TIN HIỂN THỊ
					</th>
				</tr>
				<tr>
					<td valign="top" align="right">
					Hiển thị sản phẩm:
					</td>
					<td>
					<input type="checkbox" name="state" value="1" <?php if($row->state==1) echo "checked='checked'"; ?> /> Hiển thị					
					</td>
				</tr>
				<tr>
					<td valign="top" align="right">
					Được đăng bởi:
					</td>
					<td>
					<?php echo $lists['created_by']; ?>
					</td>
				</tr>
				<tr>
					<td valign="top" align="right">
					Ngày đăng
					</td>
					<td>
					<input class="text_area" type="text" name="created" id="created" size="25" maxlength="19" value="<?php echo $ngay_nhap; ?>" />
					<input name="reset" type="reset" class="button" onclick="return showCalendar('created', 'y-mm-dd');" value="..." />
					</td>
				</tr>
				<?php
				$arrID = array();
				for ($i=0; $i<count($lsIDContent); $i++){
					array_push($arrID, $lsIDContent[$i]->content_id);
				}
				?>
				<tr>
					<td valign="top" align="right">
					Sản phẩm khuyến mãi:
					</td>
					<td>
					<select name="content_id[]" multiple="multiple" size="10">
						<?php 
						for ($i=0; $i<count($lsContentNews); $i++){
						if (in_array($lsContentNews[$i]->id, $arrID)) $selected = ' selected="selected"';
						else $selected = '';
						?>
						<option value="<?php echo $lsContentNews[$i]->id;?>"<?php echo $selected;?>><?php echo $lsContentNews[$i]->title;?></option>
						<?php }?>	
					</select>
					</td>
				</tr>				
				</table>
			</td>
		</tr>
		</table>
		
		<input type="hidden" name="id" value="<?php echo $row->id; ?>" />
		<input type="hidden" name="option" value="<?php echo $option;?>" />
		<input type="hidden" name="redirect" value="<?php echo $redirect;?>" />
		<input type="hidden" name="task" value="" />
		<input type="hidden" name="hidemainmenu" value="0" />
		<input type="hidden" name="<?php echo josSpoofValue(); ?>" value="1" />
		
		<input type="hidden" name="x1" value="" id="x1" />
		<input type="hidden" name="y1" value="" id="y1" />
		<input type="hidden" name="x2" value="" id="x2" />
		<input type="hidden" name="y2" value="" id="y2" />
		<input type="hidden" name="w" value="" id="w" />
		<input type="hidden" name="h" value="" id="h" />
		</form>
		<?php

	}
}
?>